const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    tempFilePaths: [],    //图片路径
  },

  onLoad(){

  },

  /*分享*/ 
  onShareAppMessage(){

  },

  /*显示选择上传的图片（本地或s使用相机拍照）*/
  showUploadingImg(e) {
    let _this = this, td = this.data;
    if (td.tempFilePaths.length < 6){
      wx.chooseImage({
        success: function (res) {
          // console.log("上传的图片:,", res);          
          // td.tempFilePaths.push(res.tempFiles[0])
          td.tempFilePaths = td.tempFilePaths.concat(res.tempFiles);
          _this.setData({
            tempFilePaths: td.tempFilePaths
          });
        },
      });
    }else{
      wx.showToast({
        title: "一次性最多上传6张",
        icon: "none",
        mask: true
      });
    }
  },

  /*清除图片*/
  clearImgItem(e) {
    let td = this.data, imgItem = e.currentTarget.dataset.item;
    td.tempFilePaths.splice(imgItem, 1);
    this.setData({
      tempFilePaths: td.tempFilePaths
    });
  },

  /*上传图片*/
  uploadingImg(i, successUp, failUp) {
    let _this = this, td = this.data;
    if (td.tempFilePaths.length > 0) {
      wx.uploadFile({
        url: "https://www.csdashi.cn/personal/saveFeedBackImg/",
        filePath: td.tempFilePaths[i].path,
        name: "image",
        success: function (res) {
          
          console.log("上传成功:", res);
          successUp++;
        },
        fail(res) {
          console.log("上传失败:", res);
          failUp++;
        },
        complete() {
          i++;
          console.log(i);
          if (i < td.tempFilePaths.length) {
            _this.uploadingImg(i, successUp, failUp);
          } else if (i == td.tempFilePaths.length) {
            wx.showToast({
              title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败！',
              icon: "none",
              mask: true,
              duration: 2000,
              success: function(){
                setTimeout(()=>{
                  wx.navigateBack();
                },1000);
              }
            });
          }

        }
      });
    }
  }, 

  /*手机*/
  mobile(e){
    if(e.detail.value.length > 0 && Golbal.checkPhone(e.detail.value) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
        duration: 1500
      });
    }
  }, 

  /*提交*/ 
  formSubmit(e){
    let _this = this, td = this.data, form_obj = e.detail.value;
    if(!form_obj.textarea){
      wx.showToast({
        title: "请填写反馈内容",
        icon: "none",
        mask: true
      });
    } else if (form_obj.mobile.length > 0 && Golbal.checkPhone(form_obj.mobile) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
    }else{
      SERVICE.POST(SERVICE.URL.feedBack,{
        contact: form_obj.textarea,
        feed_content: form_obj.mobile
      },(status,res)=>{
        console.log(res);
        if(res.data.status == "success"){
          wx.showToast({
            title: "提交成功",
            icon: "none",
            mask: true,
            duration: 1000,
            success:function(){
              if(td.tempFilePaths.length > 0){
                _this.uploadingImg(0, 0, 0, res.data.rental_id);
              }else{
                setTimeout(() => {
                  wx.navigateBack();
                }, 2000)
              }
            }
          });
         
        }else{
          wx.showToast({
            title: "提交失败",
            icon: "none",
            mask: true,
            duration: 1500
          });
        }
      });
    }
  }
});